package com.ltmall.api.controller;

import com.ltmall.api.bean.ClientAccount;
import com.ltmall.api.constant.FrontConstants;
import com.ltmall.api.entity.info.MemberDeductibleInfo;
import com.ltmall.api.enums.AccountPointChangeEnum;
import com.ltmall.api.manager.ClientAccountManager;
import com.ltmall.api.manager.MembershipManager;
import com.ltmall.api.util.RemoteUtil;
import com.ltmall.api.weixin.pay.utils.DateUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 用户账户管理控制器
 *
 * @author dou.guo
 * @Title ClientAccountController.java
 * @Description 类ClientAccountController.java的实现描述：类实现描述
 * @date 2017年8月11日 下午3:30:10
 * @package com.ltmall.api.controller
 */
@Controller
public class ClientAccountController {

    private ClientAccountManager clientAccountManager;

    private static final Logger logger = LogManager.getLogger(AccountController.class);

    private MembershipManager membershipManager;

    /**
     * 返回用户会员等级
     *
     * @param request
     * @param response
     */
    public void getMembershipLevel(HttpServletRequest request, HttpServletResponse response) {
        try {
            String userId = request.getParameter("userID");

            MemberDeductibleInfo info = membershipManager.getDeductible(Integer.parseInt(userId));

            RemoteUtil.response(response, info.getMembership().getCode(), FrontConstants.SUCC_CODE,
                    FrontConstants.SUCC_MESSAGE);
        } catch (Exception e) {
            logger.error("获取用户账户信息发生异常");
            RemoteUtil.response(response, null, FrontConstants.FAIL_CODE,
                    FrontConstants.FAIL_MESSAGE);
        }

    }

    /**
     * 获取用户账户信息
     *
     * @param request
     * @param response
     */
    public void getClientAccountInfo(HttpServletRequest request, HttpServletResponse response) {

        ClientAccount clientAccount = new ClientAccount();
        try {
            String userId = request.getParameter("userID");
            clientAccount = clientAccountManager.getClientAccount(new Integer(userId));
        } catch (Exception e) {
            logger.error("获取用户账户信息发生异常");
            RemoteUtil.response(response, null, FrontConstants.FAIL_CODE,
                    FrontConstants.FAIL_MESSAGE);
        }

        RemoteUtil.response(response, clientAccount, FrontConstants.SUCC_CODE,
                FrontConstants.SUCC_MESSAGE);

    }

    /**
     * 账户积分兑换懂事特权套餐
     *
     * @param request
     * @param response
     */
    public void getPrivilegeByPoint(HttpServletRequest request, HttpServletResponse response) {

        try {
            //【1】根据兑换的懂事特权
            String PrivilegeID = request.getParameter("PrivilegeID");
            String userId = request.getParameter("userID");
            //待增加的懂事特权时间
            int addtime = 0;
            //待减少的积分
            int pointDown = 0;
            switch (PrivilegeID) {
                case "1":
                    addtime = 30;
                    pointDown = 5;
                    break;
                case "2":
                    addtime = 90;
                    pointDown = 15;
                    break;
                case "3":
                    addtime = 180;
                    pointDown = 30;
                    break;
                case "4":
                    addtime = 365;
                    pointDown = 60;
                    break;
                default:
                    logger.error("用户积分兑换懂事特权错误，兑换懂事特权不在预期内。userId：" + userId);
                    RemoteUtil.response(response, null, FrontConstants.FAIL_CODE,
                            FrontConstants.FAIL_MESSAGE);
                    return;
            }

            //【2】判断该用户是否有足够的积分
            ClientAccount clientAccount = clientAccountManager
                    .getClientAccount(new Integer(userId));

            //【2.1】用户账户信息不存在
            if (clientAccount == null) {
                logger.error("用户积分兑换懂事特权错误，用户账户信息不存在。userId：" + userId);
                RemoteUtil.response(response, null, FrontConstants.FAIL_CODE,
                        FrontConstants.FAIL_MESSAGE);
                return;
            }

            //【2.2】用户积分不够
            int prePoint = clientAccount.getPoint();
            if (prePoint < pointDown) {
                logger.error("用户积分兑换懂事特权错误，用户账户积分不足。userId：" + userId);
                RemoteUtil.response(response, null, FrontConstants.FAIL_CODE,
                        FrontConstants.FAIL_MESSAGE);
                return;
            }

            //【3】更新该用户的懂事特权到期时间（懂事特权到期时间++）
            Date expiration = clientAccount.getPrivilegeExpiration();
            if (expiration == null) {
                expiration = new Date();
            }
            expiration = DateUtil.addDate(expiration, addtime);

            clientAccountManager.updateExpiration(new Integer(userId), expiration);

            //【4】更新该用户的积分（积分--）
            clientAccountManager.updateAccountPoint(new Integer(userId), pointDown,
                    AccountPointChangeEnum.POINT_DOWN);

        } catch (Exception e) {
            logger.error("获取用户账户信息发生异常");
            RemoteUtil.response(response, null, FrontConstants.FAIL_CODE,
                    FrontConstants.FAIL_MESSAGE);
            return;
        }

        RemoteUtil.response(response, null, FrontConstants.SUCC_CODE, FrontConstants.SUCC_MESSAGE);

    }

    /**
     * @param clientAccountManager the clientAccountManager to set
     */
    public void setClientAccountManager(ClientAccountManager clientAccountManager) {
        this.clientAccountManager = clientAccountManager;
    }

    /**
     * @param membershipManager the membershipManager to set
     */
    public void setMembershipManager(MembershipManager membershipManager) {
        this.membershipManager = membershipManager;
    }

}
